package com.example.soa_exp2.Controller;

import com.example.soa_exp2.Config.CommonResponse;
import com.example.soa_exp2.Domain.Return;
import com.example.soa_exp2.Domain.Borrow;
import com.example.soa_exp2.Domain.Goods;
import com.example.soa_exp2.Domain.Student;
import com.example.soa_exp2.Service.ReturnService;
import com.example.soa_exp2.Service.GoodsService;
import com.example.soa_exp2.Service.StudentService;
import com.example.soa_exp2.Service.BorrowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.Date;
import java.util.List;

@RestController
@Tag(name = "归还器材模块")
@RequestMapping("/return")
public class ReturnController {
    @Resource
    public ReturnService returnService;

    @Resource
    public BorrowService borrowService;

    @Resource
    public StudentService studentService;

    @Resource
    public GoodsService goodsService;

    @Autowired
    public Student stu;

    @Autowired
    public Goods goods;

    @Autowired
    public Return aReturn;

    @Autowired
    public Borrow borrow;

    @Operation(summary = "增加归还记录")
    @PostMapping("/{bor_id}")
    public CommonResponse<Return> AddReturnGoodsRec(@RequestParam int bor_id, @RequestParam Long re_num, @RequestParam String operator_account){

        borrow = borrowService.getBorrowById(bor_id);
        Long bor_num = borrow.getBor_num();
        if(re_num!=bor_num){
            return CommonResponse.BackForError("借用记录"+bor_id+" 归还有误 ");
        }

        else{
            stu = studentService.getStudentByNum(borrow.getStu_id());
            aReturn.setGoods_id(borrow.getGoods_id());
            aReturn.setGoods_name(borrow.getGoods_name());
            aReturn.setStu_name(stu.getName());
            aReturn.setStu_tel(stu.getTel());
            aReturn.setStu_id(borrow.getStu_id());
            aReturn.setRe_num(re_num);
            aReturn.setRe_time(new Date(System.currentTimeMillis()));
            aReturn.setOperator_account(operator_account);

            returnService.addReturn(aReturn,bor_id);
        }

        return CommonResponse.BackForSuccess("新增归还记录成功",aReturn,"http://localhost:8081/return/"+bor_id);

    }

    @Operation(summary = "查询所有归还记录")
    @GetMapping("/")
    public CommonResponse<List> GetAllReturnGoodsRec(){
        List<Return> aReturn = returnService.getReturnList();
        return CommonResponse.BackForSuccess("查询成功",aReturn,"http://localhost:8081/return");
    }

    @Operation(summary = "通过记录表的id查询归还记录")
    @GetMapping("/rec_id")
    public CommonResponse<Return> GetReturnGoodsRecById(@RequestParam("re_rec_id") int re_rec_id){
        Return aReturn = returnService.getReturnById(re_rec_id);
        return CommonResponse.BackForSuccess("查询成功",aReturn,"http://localhost:8081/return/rec_id/"+re_rec_id);
    }

    @Operation(summary = "通过学生的id查询归还记录")
    @GetMapping("/stu_id")
    public CommonResponse<List> GetReturnGoodsRecByStuId(@RequestParam("stu_id") String stu_id){
        List<Return> aReturn = returnService.getReturnByStuId(stu_id);
        return CommonResponse.BackForSuccess("查询成功",aReturn,"http://localhost:8081/return/stu_id/"+stu_id);
    }

    @Operation(summary = "通过学生的name查询归还记录")
    @GetMapping("/stu_name")
    public CommonResponse<List> GetAllReturnGoodsRecByStuName(@RequestParam("stu_name") String stu_name){
        List<Return> aReturn = returnService.getReturnByStuName(stu_name);
        return CommonResponse.BackForSuccess("查询成功",aReturn,"http://localhost:8081/return/goods_name/"+stu_name);
    }

//    @Operation(summary = "通过物资的id查询归还记录")
//    @GetMapping("/goods_id")
//    public CommonResponse<List> GetReturnGoodsRecByGoodsId(@RequestParam("goods_id") String goods_id){
//        List<Return> aReturn = returnService.getReturnByGoodsId(goods_id);
//        return CommonResponse.BackForSuccess("查询成功",aReturn,"http://localhost:8081/return/goods_id/"+goods_id);
//    }

    @Operation(summary = "通过物资的name查询归还记录")
    @GetMapping("/goods_name")
    public CommonResponse<List> GetAllReturnGoodsRecByGoodsName(@RequestParam("goods_name") String goods_name){
        List<Return> aReturn = returnService.getReturnByGoodsName(goods_name);
        return CommonResponse.BackForSuccess("查询成功",aReturn,"http://localhost:8081/return/goods_name/"+goods_name);
    }

    @Operation(summary = "通过归还表id删除归还记录")
    @DeleteMapping
    public CommonResponse<Return> GetAllReturnGoodsRecById(@RequestParam("re_rec_id") int re_rec_id){
        returnService.deleteReturnById(re_rec_id);
        return CommonResponse.BackForSuccess("删除成功","http://localhost:8081/return/"+re_rec_id);
    }

    @Operation(summary = "根据id修改归还信息")
    @PutMapping
    public CommonResponse<Return> updateReturn(@RequestParam("re_rec_id") int re_rec_id, @RequestParam("re_num") long re_num){
        if(re_rec_id<0){
            return CommonResponse.BackForError("修改信息失败");
        } else {
            aReturn = returnService.getReturnById(re_rec_id);
            aReturn.setRe_num(re_num);
            int result = returnService.updateReturnById(aReturn);
            if (result == 0) {
                return CommonResponse.BackForError("修改信息失败");
            } else {
                goodsService.addGoodsStock(aReturn.getGoods_id(),re_num-aReturn.getRe_num());
                return CommonResponse.BackForSuccess("修改归还记录成功",aReturn,"http://localhost:8081/return/"+aReturn.getRe_rec_id());
            }
        }
    }
}
